<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include_once($phpbb_root_path . 'sv_common.' . $phpEx);
include_once($phpbb_root_path . 'prophet_common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('stockvirtual');


$corp_to_watch = get_corp_to_watch();

$mode		= request_var('mode', '');
if ($mode == 'add_corp_to_watch')
{
	$corp_id		= request_var('corp_id', '');

	echo "add $corp_id to watch <BR>" . PHP_EOL;
		
	add_corp_to_watch($corp_id);
	
	$corp_to_watch[] = $corp_id;
}

// Show TWSE index info
$TAIEX_last_date	= '';
$TAIEX_first_date = '';
$query 		= 'SELECT MAX(date) as date  from taiex_index_volume ';
$result		= $db->sql_query_limit($query, 1);
if ($row = $db->sql_fetchrow($result))
{
	$TAIEX_last_date = $row['date'];

	//echo "last_date: $last_date\n";
}
$query 		= 'SELECT MIN(date) as date  from taiex_index_volume ';
$result		= $db->sql_query_limit($query, 1);
if ($row = $db->sql_fetchrow($result))
{
	$TAIEX_first_date = $row['date'];

	//echo "first_date: $first_date\n";
}
	



foreach ($corp_to_watch as $corp_id)
{

	$last_date	= '';
	$first_date = '';
	$query 		= 'SELECT MAX(date) as date  from twse_daily_trading_info WHERE corp_id="'. $corp_id . '" ';
	$result		= $db->sql_query_limit($query, 1);
	if ($row = $db->sql_fetchrow($result))
	{
		$last_date = $row['date'];
//		if ($debug)
//			echo "last_date: $last_date\n";
	}
	$query 		= 'SELECT MIN(date) as date  from twse_daily_trading_info WHERE corp_id="'. $corp_id . '"  ';
	$result		= $db->sql_query_limit($query, 1);
	if ($row = $db->sql_fetchrow($result))
	{
		$first_date = $row['date'];
//		if ($debug)
//			echo "first_date: $first_date\n";
	}
	
	$template->assign_block_vars('corp_to_watch_list', array(
		'CORP_ID'						=> $corp_id,		
		'CORP_NAME'						=> get_corp_chinese_name($corp_id),	
		'FIRST_DATE'					=> $first_date,
		'LAST_DATE'						=> $last_date,	
		)
	);	
	
}


foreach ($big_corp as $corp_id)
{

	$last_date	= '';
	$first_date = '';
	$query 		= 'SELECT MAX(date) as date  from twse_daily_trading_info WHERE corp_id="'. $corp_id . '" ';
	$result		= $db->sql_query_limit($query, 1);
	if ($row = $db->sql_fetchrow($result))
	{
		$last_date = $row['date'];
//		if ($debug)
//			echo "last_date: $last_date\n";
	}
	$query 		= 'SELECT MIN(date) as date  from twse_daily_trading_info WHERE corp_id="'. $corp_id . '"  ';
	$result		= $db->sql_query_limit($query, 1);
	if ($row = $db->sql_fetchrow($result))
	{
		$first_date = $row['date'];
//		if ($debug)
//			echo "first_date: $first_date\n";
	}
	
	$template->assign_block_vars('corp_list', array(
		'CORP_ID'						=> $corp_id,		
		'CORP_NAME'						=> get_corp_chinese_name($corp_id),	
		'FIRST_DATE'					=> $first_date,
		'LAST_DATE'						=> $last_date,	
		)
	);	
	
}

display_forums('', $config['load_moderators']);

// include nav links.
include_nav_links();

// Assign index specific vars
$template->assign_vars(array(

	'FORUM_IMG'				=> $user->img('forum_read', 'NO_NEW_POSTS'),
	'FORUM_NEW_IMG'			=> $user->img('forum_unread', 'NEW_POSTS'),
	'FORUM_LOCKED_IMG'		=> $user->img('forum_read_locked', 'NO_NEW_POSTS_LOCKED'),
	'FORUM_NEW_LOCKED_IMG'	=> $user->img('forum_unread_locked', 'NO_NEW_POSTS_LOCKED'),

	'S_LOGIN_ACTION'			=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'),
	'S_DISPLAY_BIRTHDAY_LIST'	=> ($config['load_birthdays']) ? true : false,
	'U_MONEY'					=> number_format($money),
	
	'TAIEX_FIRST_DATE'			=> $TAIEX_first_date,
	'TAIEX_LAST_DATE'			=> $TAIEX_last_date,
	
	'LOCAL_REPORT_PATH'			=> $local_report_path,
	'LOCAL_RESULT_PATH'			=> $local_result_path,
	
	
	'S_ADD_CORP_TO_WATCH_ACTION'			=> append_sid("{$phpbb_root_path}prophet_index.$phpEx", 'mode=add_corp_to_watch'),
		
	'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;mark=forums') : '',
	'U_MCP'				=> ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=front', true, $user->session_id) : '')
	
	
	
);

// Output page
page_header($user->lang['SV_RULE']);

$template->set_filenames(array(
	'body' => 'prophet_index.html')
);

page_footer();

?>